﻿@inherits TestComponentBase

<Fixture Setup="Setup" Tests="new Action[]{ InitialLoadingHtmlRendersCorrectly, AfterDataLoadsItIsDisplayedInAForecastTable }">
    <ComponentUnderTest>
        <FetchData />
    </ComponentUnderTest>
    <Fragment Id="InitialRender">
        <h1>Weather forecast</h1>
        <p>This component demonstrates fetching data from a service.</p>
        <p><em>Loading...</em></p>
    </Fragment>
    <Fragment Id="ForecastDataTableRender">
        <ForecastDataTable Forecasts="forecasts" />
    </Fragment>
</Fixture>
@code {
    private WeatherForecast[] forecasts = new[]
    {
        new WeatherForecast { Date = DateTime.Now, Summary = "Testy", TemperatureC = 42 }
    };

    private MockForecastService forecastService = new MockForecastService();

    void Setup()
    {
        Services.AddSingleton<IWeatherForecastService>(forecastService);
    }

    void InitialLoadingHtmlRendersCorrectly()
    {
        // act
        var cut = GetComponentUnderTest<FetchData>();

        // assert
        cut.MarkupMatches(GetFragment("InitialRender"));
    }

    void AfterDataLoadsItIsDisplayedInAForecastTable()
    {
        // arrange
        var expectedAddition = GetFragment("ForecastDataTableRender");
        var cut = RenderComponent<FetchData>();

        // act
        WaitForNextRender(() => forecastService.Task.SetResult(forecasts));

        // assert
        cut.GetChangesSinceFirstRender().ShouldHaveChanges(
            diff => diff.ShouldBeRemoval("<p><em>Loading...</em></p>"),
            diff => diff.ShouldBeAddition(expectedAddition)
        );
    }
}
